<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>修改数据Demo</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../assets/libs/layui/css/layui.css" media="all">
</head>

<body>

    <form class="layui-form" lay-filter="example" id="uploadForm11" enctype="multipart/form-data" method="post">

        <div class="layui-form-item" style="margin-top: 20px">
            <label class="layui-form-label">运动编号:</label>
            <div class="layui-input-block" style="margin-right: 20px">
                <input type="text" name="motionId" readonly="true" autocomplete="off" placeholder="" id="motionId"
                    class="layui-input">
            </div>
        </div>

        <div class="layui-form-item" style="margin-top: 20px">
            <label class="layui-form-label">运动姓名:</label>
            <div class="layui-input-block" style="margin-right: 20px">
                <input type="text" name="motionType" lay-verify="title" autocomplete="off" placeholder=""
                    id="motionType" class="layui-input">
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">运动热量</label>
            <div class="layui-input-block">
                <input type="text" name="motionHot" lay-verify="required" autocomplete="off" placeholder=""
                    id="motionHot" class="layui-input" type="number">
            </div>
        </div>

        <div class="layui-form-item" style="margin-top: 20px">
            <label class="layui-form-label">运动图片:</label>
            <div class="layui-input-block" style="margin-right: 20px">
                <input type="file" name="url" lay-verify="title" autocomplete="off" id="url" style="opacity: 0;" hidden>
                <img alt="" srcset="" id="urlShow" name="urlShow" width="100" height="100">
            </div>
        </div>

        <div class="layui-form-item" style="text-align: center">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" id="doSave" lay-submit="" lay-filter="demo1">修改</button>
                <button type="button" class="layui-btn layui-btn-primary">返回</button>
            </div>
        </div>
    </form>


    <script src="../../assets/libs/layui/layui.js" charset="utf-8"></script>
    <script src="../js/util.js" charset="utf-8"></script>
    <script>



        layui.config({
            base: '/module/'
        }).use(['jquery', 'layer', 'table', 'form', 'config'], function () {
            var $ = layui.jquery;
            var layer = layui.layer;
            var form = layui.form;
            var config = layui.config;
            var req = GetRequest();
            var no = req['motionId'];//获取请求的url参数editNo的值
            if (no) {
                $.ajax({
                    url: config.base_server + 'GetMotionById?editNo=' + no,
                    dataType: 'json',
                    type: 'get',
                    success: function (data) {
                        data = data.data;
                        if (!data.length) {
                            return;
                        }
                        $('#motionId').val(data[0].motionId);
                        $('#motionType').val(data[0].motionType);
                        $('#motionHot').val(data[0].motionHot);
                        $('#urlShow').attr('src', data[0].motionImg);
                    }

                })
            }

            $('#urlShow').on('click', function () {
                $('#url').trigger("click");
            })

            //保存用户数据
            form.on('submit(demo1)', function (info) {
                var data = form.val('example');
                var motionId = data.motionId;
                var motionType = data.motionType;
                var motionHot = data.motionHot;
                var url = data.url;
                let fromData = new FormData();
                fromData.append('motionId', motionId);
                fromData.append('motionType', motionType);
                fromData.append('motionHot', motionHot);
                fromData.append('motionImg', $('#url')[0].files[0])
                layer.load(2);
                $.ajax({
                    type: "POST",
                    url: config.base_server + "UpdateMotionById",
                    data: fromData,
                    async: false,
                    processData: false,   //  告诉jquery不要处理发送的数据
                    contentType: false,
                    success: function (data) {
                        if (data.code == 1) {
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                        }
                    }

                });
            })

            function checkPic(upload) {
                var valid = $(upload).attr('id');
                var picPath = document.getElementById(valid).value;
                var type = picPath.substring(picPath.lastIndexOf(".") + 1, picPath.length).toLowerCase();
                if (type !== "jpg" && type !== "bmp" && type !== "gif" && type !== "png") {
                    alert("请上传正确的图片格式");
                    return false;
                }
                return true;
            }
            function getObjectURL(file) {
                var url = null;
                if (window.createObjectURL != undefined) {
                    url = window.createObjectURL(file);
                } else if (window.URL != undefined) {
                    url = window.URL.createObjectURL(file);
                } else if (window.webkitURL != undefined) {
                    url = window.webkitURL.createObjectURL(file);
                }
                return url;
            }

            function PreviewImage(upload, width, height) {
                if (checkPic(upload)) {
                    try {
                        var imgPath = getObjectURL(upload.files[0]);
                        console.log(JSON.stringify(imgPath))
                        $('#urlShow').attr('src', imgPath);

                    } catch (e) {
                        alert("无法预览图片");
                    }
                }
            }
            $('#url').on('change', function (e) {
                PreviewImage(this, 100, 100);
            })

        });
    </script>


</body>